package com.dtw.ditouwang.ui.coin

import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.dtw.ditouwang.R
import com.dtw.ditouwang.beans.ProductBean
import com.dtw.ditouwang.models.WebApi
import com.dtw.ditouwang.ui.OnItemClickListener
import com.dtw.ditouwang.views.GlideRoundSquireTransform
import com.dtw.ditouwang.views.GlideRoundTransform
import kotlinx.android.synthetic.main.item_coin.view.*
import java.math.RoundingMode
import java.text.DecimalFormat


class CoinAdapter(var productList: ArrayList<ProductBean.DataBean>) : RecyclerView.Adapter<CoinAdapter.Holder>() {
    var requestOptions = RequestOptions.bitmapTransform(GlideRoundSquireTransform(8))
    var onItemClickListener: OnItemClickListener? = null

    override fun getItemCount(): Int {
        return productList.size
    }

    override fun onBindViewHolder(holder: Holder, position: Int) {
        Glide.with(holder.itemView.context).load(WebApi.BaseUrl + productList[position].cover.replace("\\", "/")).apply(requestOptions).into(holder.imageThine)
        holder.textName.text = productList[position].commodity_name
        holder.textMony.text = "￥ ${DecimalFormat("#.#####").format(productList[position].gold_price)}金币(${DecimalFormat("#.#####").format(productList[position].price)}元)"
    }

    override fun onCreateViewHolder(viewGroup: ViewGroup, position: Int): Holder {
        return Holder(LayoutInflater.from(viewGroup.context).inflate(R.layout.item_coin, viewGroup, false))
    }

    inner class Holder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var imageThine = itemView.imageThing
        var textName = itemView.textName
        var textMony = itemView.textMony

        init {
            itemView.setOnClickListener { onItemClickListener?.onItemClick(0, adapterPosition, 0) }
        }
    }
}
